from PIL import Image
import pytesseract
import cv2
import os
import numpy as np

preprocess = 'blur'  # thresh

pic = cv2.imread('scan.jpg')
gray = cv2.cvtColor(pic, cv2.COLOR_BGR2GRAY)#灰度图



image = cv2.resize(pic,(0, 0),fx=0.5,fy=0.5,interpolation=cv2.INTER_NEAREST)#缩小显示图像

if preprocess == "thresh":
    gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

if preprocess == "blur":
    gray = cv2.medianBlur(gray, 3)

filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)
#读取内容，保存文本
text = pytesseract.image_to_string(Image.open(filename))
print(text)
os.remove(filename)



cv2.imshow("image",image)
cv2.waitKey(0)
cv2.destroyAllWindows()
